/*

 */
package org.vectrics.graphics;

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.vectrics.SystemException;
import org.vectrics.common.graphics.Graphic;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * @author mmoore
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Main {

    
    public static void main(String[] args) {
        String fileName = "dyna-image-test.xml";
        
        DynaGraphicsManager manager = new DynaGraphicsManager();
        manager.loadDefinitions(fileName);
        Locale locale = Locale.US;
        
        renderToOutFile(manager, "std.button", locale);
        renderToOutFile(manager, "test.button", locale);
        renderToOutFile(manager, "test.rounded.button", locale);
        renderToOutFile(manager, "button.1", locale);
        renderToOutFile(manager, "button.2", locale);
        renderToOutFile(manager, "button.2.a", locale);
        renderToOutFile(manager, "button.2.b", locale);
        
        Map params = new HashMap();
        params.put("caption", "Dynamic Caption");
        renderToOutFile(manager, "button.3.b", locale, params);
    }
    
    private static void renderToOutFile(DynaGraphicsManager manager, String imageCode, Locale locale) {
        renderToOutFile(manager, imageCode, locale, null);
    }
        
    private static void renderToOutFile(DynaGraphicsManager manager, String imageCode, Locale locale, Map parameters) {
        float quality = 0.95f;
        Graphic graphic = manager.getImage(imageCode, locale, parameters);
        String fileName = imageCode + "-" + locale.getCountry().toLowerCase() + ".jpg";
        BufferedImage bufImage = graphic.fill();
        try {
	        FileOutputStream outFile = new FileOutputStream(fileName);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outFile);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufImage);
			param.setQuality(quality, true);
			encoder.encode(bufImage, param);
        } catch (Exception ex) {
            throw new SystemException("Exception saving jpeg", ex);
        }
    }
    
}
